package com.ping.redis;

import java.util.Set;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author 浩子
 * 使用原生方式操作redis  并未使用提供的redis template
 */
@Service
public class RedisManager {

	@Autowired
	private RedisConfiguration conf;

	private static JedisPool jedisPool;

	public RedisManager() {
	}

	@PostConstruct
	public void init() {
		if (jedisPool == null) {
			if (conf.getPassword() != null && !"".equals(conf.getPassword())) {
				jedisPool = new JedisPool(new JedisPoolConfig(), conf.getHost(), conf.getPort(), conf.getTimeout(),
						conf.getPassword());
			} else if (conf.getTimeout() != 0) {
				jedisPool = new JedisPool(new JedisPoolConfig(), conf.getHost(), conf.getPort(), conf.getTimeout());
			} else {
				jedisPool = new JedisPool(new JedisPoolConfig(), conf.getHost(), conf.getPort());
			}
		}
	}
	
	/**
	 * 获取值
	 * @param key
	 * @return
	 */
	 public byte[] get(byte[]key)
	    {
	        byte[] value = null;
	        Jedis jedis = jedisPool.getResource();
	        try {
	            value = jedis.get(key);
	        }finally {
	            jedis.close();
	        }
	        return value;
	    }

	    /**
	     * 设置值
	     * @param key
	     * @param value
	     * @return
	     */
	    public byte[]set(byte[]key,byte[]value)
	    {
	        Jedis jedis = jedisPool.getResource();
	        try {
	            jedis.set(key,value);
	            if(conf.getExpire() != 0)
	            {
	                jedis.expire(key,conf.getExpire());
	            }
	        }finally {
	            jedis.close();
	        }
	        return value;
	    }
	    /**
	     * set  设置过期时间
	     * @param key
	     * @param value
	     * @param expire
	     * @return
	     */
	    public byte[] set(byte[] key,byte[] value,int expire){
	        Jedis jedis = jedisPool.getResource();
	        try{
	            jedis.set(key,value);
	            if(expire != 0){
	                jedis.expire(key, expire);
	            }
	        }finally{
	            jedis.close();

	        }
	        return value;
	    }
	    /**
	     * del
	     * 删除这个key-value
	     * @param key
	     */
	    public void del(byte[] key){
	        Jedis jedis = jedisPool.getResource();
	        try{
	            jedis.del(key);
	        }finally{
	            jedis.close();

	        }
	    }
	    /**
	     * 刷新缓存
	     * flush
	     */
	    public void flushDB(){
	        Jedis jedis = jedisPool.getResource();
	        try{
	            jedis.flushDB();
	        }finally{
	            jedis.close();

	        }
	    }
	    /**
	     * 返回当前redis中key的数量
	     * size
	     */
	    public Long dbSize(){
	        Long dbSize = 0L;
	        Jedis jedis = jedisPool.getResource();
	        try{
	            dbSize = jedis.dbSize();
	        }finally{
	            jedis.close();

	        }
	        return dbSize;
	    }
	    
	    /**
	     * 获取所有的key
	     * @param pattern
	     * @return
	     */
	    public Set<byte[]> keys(String pattern){
	        Set<byte[]> keys = null;
	        Jedis jedis = jedisPool.getResource();
	        try{
	            keys = jedis.keys(pattern.getBytes());
	        }finally{
	            jedis.close();

	        }
	        return keys;
	    }

	    public static JedisPool getJedisPool() {
	        return jedisPool;
	    }

	    public static void setJedisPool(JedisPool jedisPool) {
	        RedisManager.jedisPool = jedisPool;
	    }

}
